var app = getApp();
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
  key: '37BBZ-IASRP-7VZDT-V6FJE-USUFE-OVFGR' // 必填
});
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_ding: false,//api获取
    isBottom: 0,
    navigator: { hide: 0 },
    is_answer: false,
    cates: [
      { title: '资讯', icon: 'https://www.58zltc.com/Public/quan/quan_icon1.png', url: 'news/news' },
      { title: '视频', icon: 'https://www.58zltc.com/Public/quan/quan_icon2.png', url: 'video/list' },
      { title: '话题', icon: 'https://www.58zltc.com/Public/quan/quan_icon3.png', url: 'topic/list' },
      { title: '附近', icon: 'https://www.58zltc.com/Public/quan/quan_icon4.png', url: 'member/fujin' },
      { title: '达人榜', icon: 'https://www.58zltc.com/Public/quan/quan_icon5.png', url: 'member/daren' },
      { title: '问答', icon: 'https://www.58zltc.com/Public/quan/quan_icon6.png', url: 'question/question' },
      { title: '交友', icon: 'https://www.58zltc.com/Public/quan/quan_icon7.png', url: 'member/daren' },
      { title: '圈子', icon: 'https://www.58zltc.com/Public/quan/quan_icon8.png', url: 'quan/quan' },
    ],
    flashList: [
      "https://www.58zltc.com/Uploads/2018-05-03/5aeb16a2b3d4a.jpg",
      "https://www.58zltc.com/Uploads/2018-05-03/5aeb169bcf06a.jpg",
      "https://www.58zltc.com/Uploads/2018-05-03/5aeb169370cfc.jpg",
    ],
    tabs: ['最新动态', '好友动态'],
    tabIndex: 0,
    list: [],
    topic_list: [],
    is_gift: false,
    pageNum: 1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    if (!app.globalData.userInfo) {
      app.getUserInfo();
    }
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      qid: options.id,
      myOpenid: app.globalData.openid
    })
    //快速导航
    app.ihttpGet("https://www.58zltc.com/api.php/Index/getNav", function (res) {
      var v = that.data.navigator;
      v.info = res.data
      that.setData({
        navigator: v
      })
    })
    //屏幕宽度
    wx.getSystemInfo({
      success: function (res) {
        var width = res.screenWidth;
        var v = that.data.navigator;
        v.width = width
        that.setData({
          navigator: v
        })
      },
    })

    //实例化一个动画
    this.animation = wx.createAnimation({
      // 动画持续时间，单位ms，默认值 400
      duration: 300,
      /**
       * linear 动画一直较为均匀
       * ease  从匀速到加速在到匀速
       * ease-in 缓慢到匀速
       * ease-in-out 从缓慢到匀速再到缓慢
       * step-start 动画一开始就跳到 100% 直到动画持续时间结束 一闪而过
       * step-end  保持 0% 的样式直到动画持续时间结束    一闪而过
       */
      timingFunction: 'ease',
      // 延迟多长时间开始
      delay: 100,
      /**
       * 以什么为基点做动画 效果自己演示
       * left,center right是水平方向取值，对应的百分值为left=0%;center=50%;right=100%
       * top center bottom是垂直方向的取值，其中top=0%;center=50%;bottom=100%
       */
      transformOrigin: 'right',
      success: function (res) {

      }
    })
    var width = this.data.navigator.width;
    var offset = width * 0.64;
    that.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = that.animation.export()
    v.hide = 1
    that.setData({
      //输出动画
      navigator: v
    })

    app.ihttpGet('https://www.58zltc.com/api.php/Quan/giftList', function (res) {
      that.setData({
        gift_list: res.data
      })
    });
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/getQuestion/id/' + options.id, function (res) {
      console.log(res.data)
      that.setData({
        question_item: res.data
      })
    });
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/getZanItem/qid/' + options.id + '/openid/' + app.globalData.openid, function (res) {
      that.setData({
        is_ding: res.data
      })
    });
    //参与人数
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/getJoinMans/qid/' + options.id, function (res) {
      that.setData({
        manList: res.data.list,
        manListLength: res.data.num,
        manListDing: res.data.dingNum
      })
    });
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/getQuestionsReply/qid/' + options.id + '/openid/' + app.globalData.openid, function (res) {
      var list = res.data;
      for (var i=0; i < list.length; i++) {
        list[i].my_openid = app.globalData.openid;
        list[i].item_openid = that.data.question_item.openid;
      }
      that.setData({
        list: list
      })
    });
  },

  //快捷导航
  navigator: function (e) {
    var width = this.data.navigator.width
    if (this.data.navigator.hide == 1) {
      var offset = '0'
      var mum = 0;
    } else {
      var offset = width * 0.64
      var mum = 1;
    }
    this.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = this.animation.export();
    v.hide = mum;
    this.setData({
      //输出动画
      navigator: v
    })
  },
  onShow: function () {
    let that = this;
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '刷新中..',
      mask: true
    })
    let that = this;
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/giftList', function (res) {
      that.setData({
        gift_list: res.data
      })
    });
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/getQuestion/id/' + that.data.qid, function (res) {
      that.setData({
        question_item: res.data
      })
    });
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/getZanItem/qid/' + that.data.qid + '/openid/' + app.globalData.openid, function (res) {
      that.setData({
        is_ding: res.data
      })
    });
    //参与人数
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/getJoinMans/qid/' + that.data.qid, function (res) {
      that.setData({
        manList: res.data.list,
        manListLength: res.data.num,
        manListDing: res.data.dingNum
      })
    });
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/getQuestionsReply/qid/' + that.data.qid + '/openid/' + app.globalData.openid, function (res) {
      var list = res.data;
      for (var i = 0; i < list.length; i++) {
        list[i].my_openid = app.globalData.openid;
        list[i].item_openid = that.data.question_item.openid;
      }
      that.setData({
        list: list
      })
    });
    setTimeout(function () {
      wx.hideLoading();
      wx.showToast({
        title: '刷新成功',
      })
      wx.stopPullDownRefresh()
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  goDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    
    var url = '/pages/quan/question/detailToReply?id=' + id;
   
    wx.navigateTo({
      url: url,
    })
  },
  goMember: function (e) {
    var openid = e.currentTarget.dataset.openid;
    var url = '/pages/quan/member/member?openid=' + openid;
    wx.navigateTo({
      url: url,
    })
  },
  gift: function (e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      is_gift: true,
      gift_id: id
    })
  },
  hiddenGift: function (e) {
    this.setData({
      is_gift: false
    })
  },
  setGift: function (e) {
    var idx = e.currentTarget.dataset.idx;
    let that = this;
    var gift_list = that.data.gift_list;
    gift_list.forEach(function (value, key) {
      gift_list[key].active = key == idx ? true : false;
    })
    this.setData({
      gift_list: gift_list,
      active_gift: gift_list[idx]
    })
  },
  payGift: function (e) {
    let that = this;
    var data = this.data;
    var post_data = {};

    data.list.forEach(function (value, idx) {
      if (value.id == data.gift_id) {
        post_data.to = value.follow_openid;
      }
    })
    post_data.type = 'quan';
    post_data.from = app.globalData.openid;
    post_data.id = data.gift_id;
    post_data.gift_id = data.active_gift.id;

    app.ihttpPost('https://www.58zltc.com/api.php/Quan/payGift', post_data, function (res) {
      if (res.data.err_code == 400) {
        wx.showToast({
          title: res.data.err_msg,
        })
      } else {
        var data = res.data;
        wx.requestPayment({
          'appId': data.appId,
          'timeStamp': data.timeStamp,
          'nonceStr': data.nonceStr,
          'package': data.package,
          'signType': 'MD5',
          'paySign': data.paySign,
          'success': res => {
            if (res.errMsg == 'requestPayment:ok') {
              wx.showToast({
                title: '赠送成功！',
              })
            }
          }
        })
        that.reBg();
      }
    })
  },
  zan: function (e) {
    
    var id = e.currentTarget.dataset.id;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    var list = this.data.list;
    var that = this;
    wx.request({
      url: 'https://www.58zltc.com/api.php/Quan/anwerZan?openid=' + app.globalData.openid + '&id=' + id + '&token=' + extConfig.appid,
      success: res => {
       
        if (res.data.err_code !== 0) {
          wx.showToast({
            title: res.data.err_msg
          })
        } else {
          list.forEach(function (vo, idx) {
            if (vo.id == id) {
              if (list[idx].is_zan) {
                list[idx].is_zan = false;
                list[idx].zan_num = parseInt(list[idx].zan_num) - 1;
              } else {
                list[idx].is_zan = true;
                list[idx].zan_num = parseInt(list[idx].zan_num) + 1;
              }
            }
            that.setData({
              list: list
            })
          })
        }
      }
    })
  },

  showImgs: function (e) {
    const pics = e.currentTarget.dataset.pics;
    const first_pics = e.currentTarget.dataset.image;
    wx.previewImage({
      urls: pics,
      current: first_pics
    })
  },
  answerQuestionTxt: function (e) {
    this.setData({
      answer_id: e.currentTarget.dataset.id,
      is_answer: true
    })
  },
  answerSonQuestionTxt: function (e) {
    this.setData({
      answerSon_id: e.currentTarget.dataset.id,
      is_answerSon: true
    })
  },
  bindblur: function (e) {
    this.setData({
      is_answer: false
    })
  },
  bindblurSon: function (e) {
    this.setData({
      is_answerSon: false
    })
  },
  formSubmitpP: function (e) {
    wx.showLoading({
      title: '加载中..'
    })
    let that =this;
    var list = this.data.list;
    var post_data = e.detail.value;
    if (post_data.content == '') {
      wx.showToast({
        title: '评论内容不能为空',
        icon: 'none',
        duration: 1000,
        mask: true
      })
    } else {
      post_data.qid = this.data.answer_id;
      post_data.openid = app.globalData.openid;
      app.ihttpPost('https://www.58zltc.com/api.php/Quan/addReply', post_data, function (res) {
        if (res.data) {
          app.ihttpGet('https://www.58zltc.com/api.php/Quan/getQuestionsReply/qid/' + that.data.qid + '/openid/' + app.globalData.openid, function (res) {
            wx.hideLoading();
            if (res.data) {
              wx.showToast({
                title: '评论成功',
                duration: 1000,
                mask: true
              })
              var list = res.data;
              for (var i = 0; i < list.length; i++) {
                list[i].my_openid = app.globalData.openid;
                list[i].item_openid = that.data.question_item.openid;
              }
              that.setData({
                list: list
              })
            }
          });
        }
      })
    }
  },
  formSubmitpSP: function (e) {
    wx.showLoading({
      title: '加载中..'
    })
    let that = this;
    var post_data = e.detail.value;
    if (post_data.content == '') {
      wx.showToast({
        title: '评论内容不能为空',
        icon: 'none',
        duration: 1000,
        mask: true
      })
    } else {
      post_data.qid = that.data.qid;
      post_data.pid = this.data.answerSon_id;
      post_data.openid = app.globalData.openid;
      wx.showLoading({
        title: '加载中..',
      })
      app.ihttpPost('https://www.58zltc.com/api.php/Quan/addReplySon', post_data, function (res) {
        if (res.data) {
          app.ihttpGet('https://www.58zltc.com/api.php/Quan/getQuestionsReply/qid/' + that.data.qid + '/openid/' + app.globalData.openid, function (res) {
            wx.hideLoading();
            if (res.data) {
              wx.showToast({
                title: '评论成功',
                duration: 1000,
                mask: true
              })
              var list = res.data;
              for (var i = 0; i < list.length; i++) {
                list[i].my_openid = app.globalData.openid;
                list[i].item_openid = that.data.question_item.openid;
              }
              that.setData({
                list: list
              })
            }
          });
        }
      })
    }
  },
  changDing: function () {
    let that = this;
    var ding = this.data.is_ding
    this.data.is_ding ? this.setData({ is_ding: 0, manListDing: that.data.manListDing - 1 }) : this.setData({ is_ding: 1, manListDing: that.data.manListDing + 1 });
    app.ihttpGetOutLoading('https://www.58zltc.com/api.php/Quan/changeDing/qid/' + that.data.qid + '/openid/' + app.globalData.openid + '/ding/' + ding, function (res) {
      if (!res.data) {
        wx.showToast({
          title: '网络繁忙',
          icon: 'none',
          duration: 1000,
          mask: true
        })
      }
    });
  },
  changCaiNa: function (e) {
    let that = this;
    var id = e.target.dataset.id;
    wx.showLoading({
      title: '加载中..',
    })
    app.ihttpGetOutLoading('https://www.58zltc.com/api.php/Quan/changeCaiNa/id/' + id, function (res) {
      if (!res.data) {
        wx.hideLoading();
        wx.showToast({
          title: '网络繁忙',
          icon: 'none',
          duration: 1000,
          mask: true
        })
      } else {
        app.ihttpGet('https://www.58zltc.com/api.php/Quan/getQuestionsReply/qid/' + that.data.qid + '/openid/' + app.globalData.openid, function (res) {
          wx.hideLoading();
          that.setData({
            list: res.data
          })
        });
      }
    });
  },
  backto: function (e) {
    wx.navigateBack({})
  }
})